package cn.scs.common;

import com.sun.deploy.util.StringUtils;
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
import com.sun.xml.internal.ws.util.xml.XmlUtil;
import org.dom4j.*;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.BaseElement;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ImageInfo {

    private String id;    // 虚拟磁盘文件 id号
    private String eid;    // 对应的edit文件 id号
    private String path;    // 对应的路径目录
    private int frequency;  // 近一个周期该盘的挂载使用频数 (不包含自动挂载)
    private long size;      // 虚拟磁盘元数据大小
    private long last;  // 最后加载该虚拟盘的时间戳
    private List<String> pwd = new ArrayList<>();

    // 实例化
    public ImageInfo(){

    }
    public ImageInfo(String path, String id, String editorId) {
        this.path = path;
        this.id = id;
        this.eid = editorId;
        this.size = -1;
        this.last = System.currentTimeMillis();
        this.frequency = 1;
        Random rand = new Random();
        for (int i=0;i<6;i++) {
            pwd.add( String.valueOf(rand.nextInt(7)+2) );
        }
    }
    // 实例化重载 加载ImageInfo Element
    public ImageInfo(Element info){
        this.id = info.element("id").getText();
        this.eid = info.element("eid").getText();
        this.path = info.element("path").getText();
        this.size = Long.parseLong( info.element("size").getText() );
        this.last = Long.parseLong( info.element("last").getText() );
        this.frequency = Integer.parseInt( info.element("frequency").getText() );
    }

    // XML Element化
    public Element asElement() {
        Element node_Image = new BaseElement("image");
        node_Image.addElement("id").setText(this.id);
        node_Image.addElement("eid").setText(this.eid);
        node_Image.addElement("path").setText(this.path);
        node_Image.addElement("size").setText( String.valueOf(this.size) );
        node_Image.addElement("last").setText( String.valueOf(this.last) );
        node_Image.addElement("frequency").setText( String.valueOf(this.frequency) );
        return node_Image;
    }
    // 更新Element
    public void updateElement(Element img) {
        img.element("eid").setText(this.eid);
        img.element("path").setText(this.path);
        img.element("size").setText( String.valueOf(this.size) );
        img.element("last").setText( String.valueOf(this.last) );
        img.element("frequency").setText( String.valueOf(this.frequency) );
    }

    // 保存XML
    public static void saveMap(Document dom, String path) {
        try {
            //设置 缩进输出 与 编码格式
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter writer = new XMLWriter(new FileWriter(path),format);
            //写入数据
            writer.write(dom);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 重载创建空XML
    public static void saveMap(String path, String root) {
        // 添加 map根节点 初始化
        Document document = DocumentHelper.createDocument();
        document.addElement(root);
        ImageInfo.saveMap(document, path);
    }

    // 输出至 map.xml 管理
    public void toMap(String mapPath) {
        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(mapPath));
            Element map = document.getRootElement();
            System.out.println("./image[id=" + this.id + "]");
            // 查找是否存在此Image元素
            Element oldImage = (Element) map.selectSingleNode("./image[id='" + this.id + "']");
            if (oldImage != null) {
                // 存在则替换
                System.out.println("Finded!");
                updateElement(oldImage);
            } else {
                // 向root新增 重载支持Element
                map.add( this.asElement() );
            }
            // 保存
            ImageInfo.saveMap(document, mapPath);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    // 更新 依据 ImageInfo 对应 metaPath 下的文件信息, 更新相关的属性值
    public void updateSize(String metaPath) {
        this.size = new File(metaPath, this.id+".image").length();
    }

    @Override
    public String toString() {
        return "ImageInfo :" + "{ id: " + this.id + "; "  + "path: " + this.path + "; " + "pwd: " + StringUtils.join(this.pwd, ",") + "}";
    }

    // Getter & Setter
     // region

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEid() {
        return eid;
    }

    public void setEid(String eid) {
        this.eid = eid;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    public long getLast() {
        return last;
    }

    public void setLast(long last) {
        this.last = last;
    }

    public int getFrequency() {
        return frequency;
    }

    public void setFrequency(int frequency) {
        this.frequency = frequency;
    }

    // endregion
}
